﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Zcu.PanelComposer.Interfaces;
using Zcu.PanelComposer.ViewModels.Core;
using Zcu.PanelComposer.Models.Model;
using Caliburn.Micro;
using Zcu.PanelComposer.ViewModels.Events;

namespace Zcu.PanelComposer.ViewModels.Pages
{
    /// <summary>
    /// Prehled solutionu.
    /// </summary>
    [Export(typeof(IModule))]
    public class ExistingSolutionsViewModel : NavigateScreen
    {
        #region Fields

        private Solution _SelectedSolution;
        private BindableCollection<Solution> _Solution;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the repository.
        /// </summary>
        [Import]
        public IRepository Repository { get; private set; }

        /// <summary>
        /// Gets or sets the selected solution.
        /// </summary>
        /// <value>
        /// The selected solution.
        /// </value>
        public Solution SelectedSolution
        {
            get { return _SelectedSolution; }
            set
            {
                if (_SelectedSolution != value) _SelectedSolution = value;
                NotifyOfPropertyChange(() => SelectedSolution);

                NotifyOfPropertyChange(() => CanActiveAction);
                NotifyOfPropertyChange(() => CanRemoveAction);
            }
        }

        /// <summary>
        /// Gets or sets the solution.
        /// </summary>
        /// <value>
        /// The solution.
        /// </value>
        public BindableCollection<Solution> Solution
        {
            get { return _Solution; }
            set
            {
                if (_Solution != value) _Solution = value;
                NotifyOfPropertyChange(() => Solution);
            }
        }

        #endregion

        #region Guard properties

        /// <summary>
        /// Zdali lze aktivovat solution.
        /// </summary>
        public bool CanActiveAction { get { return SelectedSolution != null; } }

        /// <summary>
        /// Zdali lze smazat solution.
        /// </summary>
        public bool CanRemoveAction { get { return SelectedSolution != null; } }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ExistingSolutionsViewModel"/> class.
        /// </summary>
        public ExistingSolutionsViewModel()
        {
            /// todo do resources
            DisplayName = "Existing Solutions";
            ModuleName = PageNames.ExistingSolutions;
            Order = 3;
        }

        #endregion

        #region Overrided methods

        /// <summary>
        /// Called when activating.
        /// </summary>
        protected override void OnActivate()
        {
            base.OnActivate();

            // nacteni dat
            LoadData();
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Akce, vyvolana stiskem tlacitka "Active".
        /// </summary>
        public void ActiveAction()
        {
            // zmena aktivniho projektu
            EventAggregator.Publish(new ActiveSolutionEvent(SelectedSolution));
            Navigate(PageNames.SolutionProjects);
        }

        /// <summary>
        /// Odstraneni projektu.
        /// </summary>
        public void RemoveAction()
        {
            // odmazeme vsechny vazby
            var solutionProjects = Repository.GetAll<SolutionProject>(s => s.Solution == SelectedSolution);
            foreach (var solutionProject in solutionProjects)
            {
                Repository.Delete<SolutionProject>(solutionProject);
            }
            
            // pak solution
            Repository.Delete<Solution>(SelectedSolution);
            Repository.Commit();

            // deaktivujeme solution
            EventAggregator.Publish(new ActiveSolutionEvent(null));
            // prenacteme data
            LoadData();
        }

        #endregion // Public methods

        #region Private methods

        /// <summary>
        /// Loads the data.
        /// </summary>
        private void LoadData()
        {
            Solution = new BindableCollection<Solution>(Repository.GetAll<Solution>());
        }

        #endregion
    }
}
